package main

import (
	"bufio"
	"log"
	"net"
	"strconv"
	"sync"
)

func main() {
	client, err := net.Dial("tcp", "127.0.0.1:2000")
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	var wg sync.WaitGroup
	wg.Add(2)

	go func(conn net.Conn, wg *sync.WaitGroup) {
		defer wg.Done()
		for i := 10; i > 0; i-- {
			_, err := conn.Write([]byte("hello" + strconv.Itoa(i) + "\r\n"))

			if err != nil {
				log.Println("Error to send message because of ", err.Error())
				break
			}
		}
	}(client, &wg)

	go func(conn net.Conn, wg *sync.WaitGroup) {
		defer wg.Done()
		reader := bufio.NewReader(conn)
		for i := 1; i <= 10; i++ {
			line, err := reader.ReadString(byte('\n'))
			if err != nil {
				log.Print("Error to read message because of ", err)
				return
			}
			log.Println("read", line)
		}
	}(client, &wg)

	wg.Wait()
}
